package com.lenovo.spider.support;

import com.lenovo.spider.config.ConfigHolder;
import com.lenovo.spider.support.cookie.CookieStore;
import com.lenovo.spider.support.cookie.FileCookieStore;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.val;

import java.util.function.Supplier;

/**
 * @author <a href="mailto:liuhan3@lenovo.com">HanL(liuhan3)</a>
 * on 2018/3/30 11:21
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DefaultFactories {

    /**
     * 默认的cookieStore工厂
     */
    public static final Supplier<CookieStore> COOKIESTORE_FACTORY = () -> {
        val fileCookieStore = "downloader.cookieStore.file";
        val redisCookieStore = "downloader.cookieStore.redis";
        if (ConfigHolder.has(fileCookieStore)) {
            return new FileCookieStore(ConfigHolder.getString(fileCookieStore));
        } else if (ConfigHolder.has(redisCookieStore)) {
            throw new UnsupportedOperationException("no support");
        } else {
            throw new RuntimeException("can not create CookieStore, please check configuration of downloader.cookieStore");
        }
    };

}
